package com.imoblife.brainwavestus.oldLibraryClass;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.xmlpull.v1.XmlPullParserException;

import com.imoblife.brainwavestus.BrainWavestusApplication;
import com.imoblife.brainwavestus.ProductDetailActivity;
import com.imoblife.brainwavestus.R;
import com.imoblife.brainwavestus.UserProductActivity;
import com.imoblife.brainwavestus.customview.AbsProductList;
import com.imoblife.brainwavestus.customview.AbsProductList.OnItemListener;
import com.imoblife.brainwavestus.customview.BasicDisplay;
import com.imoblife.brainwavestus.model.Constant;
import com.imoblife.brainwavestus.product.Product;
import com.imoblife.brainwavestus.product.Store;
import com.imoblife.brainwavestus.util.BitMapFlyweightFactory;
import com.imoblife.brainwavestus.util.DebugUtil;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class AbsCategoryProductList extends BasicDisplay {

	private static final String TAG = "AbsCategoryProductList";

	Cursor mProductCursor;

	List<String> mCategoryList;

	AbsProductList mAbsProductList;

	HashMap<String, String> mtitleBuffer = new HashMap<String, String>();
	
	private Store mStore;
	private ArrayList<String> mCategories;
	private String mCurrentCategory;
	
	protected void initializeXlargerProductList() {
		
		mAbsProductList = (AbsProductList) mActivity
				.findViewById(R.id.product_list);
		mAbsProductList.release();
		mAbsProductList.setVisibility(View.VISIBLE);
		long prev = System.currentTimeMillis();
		
		List<String> list = new ArrayList<String>();
		
		ArrayList<Product> products 
			= mStore.getProductByCategory(mCurrentCategory);
		
		for(Product product : products) {
			
			String pId = product.getProductInfo().getId();
			list.add(pId);
			DebugUtil.debug(TAG, "add pad product Id: " + pId);
		}
		
		mAbsProductList.setProductLayout(R.layout.tb_product_icon);

		prev = System.currentTimeMillis();

		mAbsProductList.setDataList(list);

		mAbsProductList.setOnItemClicker(new OnItemListener() {

			public void onItemClick(View view, String productId) {

				Intent intent = new Intent(mActivity, ProductDetailActivity.class);
				intent.putExtra(Constant.PRODUCT_ID, productId);
				mActivity.startActivity(intent);
			}

			public void onItemLoading(View parent, String productId) {

				ImageView productIcon = (ImageView) parent
						.findViewById(R.id.product_icon);

				productIcon.setImageBitmap(BitMapFlyweightFactory
						.getBitMapFlyweightFactory(mActivity.getAssets())
						.getBitMapFlyweight(productId).getBitmap());

				LayoutParams params = productIcon.getLayoutParams();

				if (parent.getLayoutParams() != null) {
					params.width = parent.getLayoutParams().height
							- parent.getPaddingTop()
							- parent.getPaddingBottom();
					params.width *= 0.8;
					params.height = params.width;
					productIcon.setLayoutParams(params);
				}

				TextView productName = (TextView) parent
						.findViewById(R.id.product_name);
				String title = mStore.getProductByName(productId)					
						.getProductInfo().getTitle();
				productName.setText(title);
			}
		});
		System.gc();
	}

	/**
	 * ��ʼ����Ʒ�б�
	 */
	protected void initializeProductList() {
		mAbsProductList = (AbsProductList) mActivity
				.findViewById(R.id.product_list);
		mAbsProductList.removeAllViews();
		mAbsProductList.setVisibility(View.VISIBLE);
		long prev = System.currentTimeMillis();
		List<String> list = new ArrayList<String>();
		while (mProductCursor.moveToNext()) {
			String key = mProductCursor.getString(1);
			String title = mProductCursor.getString(2);
			list.add(key);
			mtitleBuffer.put(key, title);

		}
		mProductCursor.close();
		DebugUtil.debug(TAG, "����������>>" + (System.currentTimeMillis() - prev));

		mAbsProductList.setProductLayout(R.layout.product_icon);

		prev = System.currentTimeMillis();

		DebugUtil.debug(TAG, "ɾ���>>" + (System.currentTimeMillis() - prev));
		mAbsProductList.setDataList(list);

		mAbsProductList.setOnItemClicker(new OnItemListener() {

			public void onItemClick(View view, String productId) {

				Intent intent = new Intent(mActivity, ProductDetailActivity.class);
				intent.putExtra("name", productId);
				mActivity.startActivity(intent);
			}

			public void onItemLoading(View parent, String productId) {

				ImageView productIcon = (ImageView) parent
						.findViewById(R.id.product_icon);

				productIcon.setImageBitmap(BitMapFlyweightFactory
						.getBitMapFlyweightFactory(mActivity.getAssets())
						.getBitMapFlyweight(productId).getBitmap());

				LayoutParams params = productIcon.getLayoutParams();

				if (parent.getLayoutParams() != null) {
					params.width = parent.getLayoutParams().height
							- parent.getPaddingTop()
							- parent.getPaddingBottom();
					DebugUtil.debug(TAG, "w=" + params.width);
					DebugUtil.debug(TAG, "h=" + params.height);
					params.width *= 0.8;
					params.height = params.width;
					productIcon.setLayoutParams(params);
				}

				TextView productName = (TextView) parent
						.findViewById(R.id.product_name);
				productName.setText(mtitleBuffer.get(productId));
				DebugUtil.debug("test",
						productId + "title=" + mtitleBuffer.get(productId));

			}
		});
		System.gc();
	}

	/**
	 * ��ʼ�������б�
	 */
	protected void initializeCategoryList() {
		
		BrainWavestusApplication app 
			= (BrainWavestusApplication) mActivity.getApplication();
		mStore = app.getStore();
		mCategories = mStore.getAllCategories();
		mCategories.add(0, mActivity.getString(R.string.all_category));

		initializeXLargerCategoryList();
	
	}

	ArrayAdapter<String> mCategoryAdapter;

	/**
	 * ��ʼ�������w����б�
	 */
	protected void initializeXLargerCategoryList() {
		
		
		mCategoryAdapter = new ArrayAdapter<String>(mActivity,
				R.layout.tb_category_item) {

			public View getView(int position, View convertView, ViewGroup parent) {

				TextView button;
				if (convertView == null) {

					button = (TextView) mActivity.getLayoutInflater().inflate(
							R.layout.tb_category_item, parent, false);

				} else {
					button = (TextView) convertView;
				}

				button.setText(getItem(position));
				return button;
			}

		};
		
		mCategoryAdapter.addAll(mCategories);
		
		final Button spinner = (Button) mActivity
				.findViewById(R.id.categoryLayout);
		spinner.setText((String) mCategoryAdapter.getItem(0));
		spinner.setVisibility(View.VISIBLE);

		spinner.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				initCategoryButton(spinner);
			}
		});
		mCurrentCategory = mCategories.get(0);
		initializeXlargerProductList();

	}

	private void initCategoryButton(final Button view) {
		View rootView = mActivity.getLayoutInflater().inflate(
				R.layout.popwindow, null);
		ListView listView = (ListView) rootView.findViewById(R.id.mylist);
		listView.setAdapter((ListAdapter) mCategoryAdapter);
		final PopupWindow popupWindow = new PopupWindow(view.getWidth() - 2,
				LayoutParams.WRAP_CONTENT);
		popupWindow.setFocusable(true);
		popupWindow.setContentView(rootView);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());

		popupWindow.showAsDropDown(view, 1, 0);
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				mCurrentCategory = mCategories.get(arg2);
				initializeXlargerProductList();
				view.setText((String) mCategoryAdapter.getItem(arg2));
				popupWindow.dismiss();
			}
		});

	}

	/**
	 *
	 * 
	 * @return
	 * @throws XmlPullParserException
	 * @throws IOException
	 */
	protected ArrayAdapter<String> getCategoryList() {
		mCategoryList = new ArrayList<String>();
		
		ArrayAdapter<String> arrayList = null;
		
		if (isXLargerScreen()) {
			arrayList = new ArrayAdapter<String>(mActivity,
					R.layout.tb_category_item) {

				public View getView(int position, View convertView,
						ViewGroup parent) {

					TextView button;
					if (convertView == null) {

						button = (TextView) mActivity.getLayoutInflater()
									.inflate(R.layout.tb_category_item, parent,
											false);						

					} else {
						button = (TextView) convertView;
					}

					button.setText(getItem(position));
					return button;
				}

			};
		} 
//		SycSqlite sqlite = SQLiteManager.getDatabaseInstence(mActivity);
//		Cursor cursor = sqlite.query(ShopTable.TB_NAME,
//				new String[] { ShopTable.CATEGORY }, null, null,
//				ShopTable.CATEGORY, null, null);
		Cursor cursor = null;
		DebugUtil.debug("test", "size=" + cursor.getCount());
		arrayList
				.add(mActivity.getResources().getString(R.string.all_category));
		mCategoryList.add(mActivity.getResources().getString(
				R.string.all_category));
		while (cursor.moveToNext()) {
			arrayList.add(cursor.getString(0));
			DebugUtil.debug("test", "item=" + cursor.getString(0));
			mCategoryList.add(cursor.getString(0));
		}
		cursor.close();
		return arrayList;

	}

	public AbsCategoryProductList(Activity activity) {
		super(activity);

	}

	public void loading() {
		initializeCategoryList();
	}

	public boolean finish() {

		if (mAbsProductList == null) {
			return false;

		}

		if (!isXLargerScreen()
				&& ((View) (mAbsProductList.getParent())).getVisibility() == View.VISIBLE) {

			mActivity.findViewById(R.id.title).setVisibility(View.GONE);
			((View) (mAbsProductList.getParent())).setVisibility(View.GONE);
			mActivity.findViewById(R.id.category_list).setVisibility(
					View.VISIBLE);
			System.gc();
			mAbsProductList.release();
			return true;
		}

		if (isXLargerScreen()) {
			mAbsProductList.release();
		}

		return false;

	}

	public void release() {
		if (mAbsProductList != null) {
			mAbsProductList.release();
		}
	}
}
